package cn.edu.cdp.gui.frame;

import java.util.*;
import cn.edu.cdp.dao.AccountDao;
import cn.edu.cdp.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@Component
public class LoginFrame extends JFrame {
    private final MainFrame mainFrame;
    private final AccountDao accountDao;

    @Autowired
    public LoginFrame(MainFrame mainFrame, AccountDao accountDao) {
        initComponents();
        this.mainFrame = mainFrame;
        this.accountDao = accountDao;

//        textFieldUsername.setText("2206631602");
//        textFieldUsername.setText("c001");
        textFieldUsername.setText("admin");
        passwordField.setText("1");
    }

    private void login(ActionEvent e) {
        String username = textFieldUsername.getText();
        String password = new String(passwordField.getPassword());
        Account account = accountDao.login(username, password);

        if (account != null) {
            mainFrame.decoration(account);
            mainFrame.setVisible(true);
            this.dispose();
        }
    }

    



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        ResourceBundle bundle = ResourceBundle.getBundle("swing_text");
        panel1 = new JPanel();
        label1 = new JLabel();
        textFieldUsername = new JTextField();
        label2 = new JLabel();
        passwordField = new JPasswordField();
        buttonLogin = new JButton();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new GridBagLayout());
            ((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {45, 45, 45, 45, 45, 40, 0};
            ((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {25, 25, 35, 20, 0};
            ((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
            ((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};

            //---- label1 ----
            label1.setText(bundle.getString("LoginFrame.label1.text"));
            panel1.add(label1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));
            panel1.add(textFieldUsername, new GridBagConstraints(2, 0, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));

            //---- label2 ----
            label2.setText(bundle.getString("LoginFrame.label2.text"));
            panel1.add(label2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));
            panel1.add(passwordField, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));

            //---- buttonLogin ----
            buttonLogin.setText(bundle.getString("LoginFrame.buttonLogin.text"));
            buttonLogin.addActionListener(e -> login(e));
            panel1.add(buttonLogin, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));
        }
        contentPane.add(panel1, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JLabel label1;
    private JTextField textFieldUsername;
    private JLabel label2;
    private JPasswordField passwordField;
    private JButton buttonLogin;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
